<?php
namespace App\Http\Controllers\Lottery;

use App\Models\Lottery\Lottery;
use App\Models\Lottery\LotteryStatistics;
use App\Models\Order\Order;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use App\Models\Agent\ChaoZongdai;
class LotteryController extends Controller
{
    public function index(Request $request)
    {
        if ($request->ajax()) {

            $Lottery = $this->filters($request, new Lottery());
            $paginator = $Lottery->orderBy($request->get("sidx"), $request->get("sord"))->paginate($request->get("rows"));

            $rows = [];
            foreach ($paginator as $page) {
                $rows[] = $page;
            }

//            if ($rows) {
//                $ids = [];
//                foreach ($rows as $p) {
//                    $ids[] = $p->issue;
//                }
//
//                $counts1 = Order::whereIn("number", $ids)->where("type", 1)->select("number", DB::raw("SUM(num*amounts) as c"))->groupBy("number")->get();
//                $counts2 = Order::whereIn("number", $ids)->where("type", 2)->select("number", DB::raw("SUM(num*amounts) as c"))->groupBy("number")->get();
//            }
//
//            foreach ($rows as $p) {
//                $p->c1 = 0;
//                foreach ($counts1 as $p2) {
//                    if ($p->issue == $p2->number) {
//                        $p->c1 = $p2->c;
//                    }
//                }
//                $p->c2 = 0;
//                foreach ($counts2 as $p2) {
//                    if ($p->issue == $p2->number) {
//                        $p->c2 = $p2->c;
//                    }
//                }
//            }

            $data = [
                'rows' => $rows,
                'total'=> $paginator->lastPage(),
                'page' => $paginator->currentPage(),
                'records' => $paginator->count(),
            ];

            return response()->json($data);

        } else {
            return view('lottery/index');
        }
    }

    public function day(Request $request)
    {
        if ($request->ajax()) {

            $LotteryStatistics = $this->filters($request, new LotteryStatistics());
            $paginator = $LotteryStatistics->orderBy($request->get("sidx"), $request->get("sord"))->paginate($request->get("rows"));

            $rows = [];
            foreach ($paginator as $page) {
                $rows[] = $page;
            }

            $data = [
                'rows' => $rows,
                'total'=> $paginator->lastPage(),
                'page' => $paginator->currentPage(),
                'records' => $paginator->count(),
            ];

            return response()->json($data);

        } else {
            return view('lottery/day');
        }
    }

    public function touzhu(Request $request)
    {   
        if($request->ajax()) {

            if ($this->isChaoZongdai()) {
                $admin = Auth::user();
                $ChaoZongdai= ChaoZongdai::where("email", $admin->email)->first();
            }

            if ($request->get("date")){
                $cond = '日期:' . $request->get("date");
                $result = Order::select('id','num','amounts','type','res','discount_type')->whereBetween("open_time", [$request->get("date"), date('Y-m-d',strtotime($request->get("date") .'+1 day'))])->get();
                if ($this->isChaoZongdai()) {
                    $result = Order::select('id','num','amounts','type','res','discount_type')->whereBetween("open_time", [$request->get("date"), date('Y-m-d',strtotime($request->get("date") . '+1 day'))])->where('chao_zongdai',$ChaoZongdai->id)->get();
                }
            }else{
                if ($request->get("number")) {
                    $number = $request->get("number");
                    if ($request->get("chao_zongdai")){
                        $cond = '合伙人id:' . $request->get("chao_zongdai") . '   期号:'. $number;
                        if ($this->isChaoZongdai()) {
                            $result = Order::where('number',$number)->where('chao_zongdai',$ChaoZongdai->id)->select('id','num','amounts','type','res','discount_type')->get();
                        } else{
                            $result = Order::where('number',$number)->where('chao_zongdai',$request->get("chao_zongdai"))->select('id','num','amounts','type','res','discount_type')->get();
                        }
                    }elseif ($request->get("zongdai")){
                        $cond = '伙伴id:' . $request->get("zongdai") . '   期号:'. $number;
                        if ($this->isChaoZongdai()) {
                            $result = Order::where('number',$number)->where('zongdai',$request->get("zongdai"))->where('chao_zongdai',$ChaoZongdai->id)->select('id','num','amounts','type','res','discount_type')->get();
                        }else{
                            $result = Order::where('number',$number)->where('zongdai',$request->get("zongdai"))->select('id','num','amounts','type','res','discount_type')->get();
                        }
                    }else{
                        $cond = '期号:' . $number;
                        if ($this->isChaoZongdai()) {
                            $result = Order::where('number',$number)->where('chao_zongdai',$ChaoZongdai->id)->select('id','num','amounts','type','res','discount_type')->get();
                        }else {
                            $result = Order::where('number',$number)->select('id','num','amounts','type','res','discount_type')->get();
                        }
                    }
                }else {
                    $last = DB::table('shishicai')->orderBy("id", "desc")->first();
                    $second = 3 * 60 + 30;
                    $beishu = (time() - strtotime($last->opentime))/ $second;
                    $beishu = ceil($beishu);
                    if ($beishu > 2) {
                        $beishu = 2;
                    }
                    $number = $last->issue + $beishu;
                    $cond = '期号' . $number;
                    if ($this->isChaoZongdai()) {
                        $result = Order::where('number',$number)->where('chao_zongdai',$ChaoZongdai->id)->select('id','num','amounts','type','res','discount_type')->get();
                    } else {
                        $result = Order::where('number',$number)->select('id','num','amounts','type','res','discount_type')->get();
                    }
                }
            }

            $shu1 = ["ids" => [] , "cond"=>$cond, "name" => "数字1" , "type" => 0 , 
                0=>0 , 1=>0 , 2=>0 , 3=>0 , 4=>0 , 5=>0 , 6=>0 , 7=>0 , 8=>0 , 9=>0,
                'ids0'=>[], 'ids1'=>[], 'ids2'=>[], 'ids3'=>[], 'ids4'=>[], 'ids5'=>[], 'ids6'=>[], 'ids7'=>[], 'ids8'=>[], 'ids9'=>[]
            ];
            $shu2 = ["ids" => [] , "cond"=>$cond, "name" => "数字2" , "type" => 0 , 
                0=>0 , 1=>0 , 2=>0 , 3=>0 , 4=>0 , 5=>0 , 6=>0 , 7=>0 , 8=>0 , 9=>0,
                'ids0'=>[], 'ids1'=>[], 'ids2'=>[], 'ids3'=>[], 'ids4'=>[], 'ids5'=>[], 'ids6'=>[], 'ids7'=>[], 'ids8'=>[], 'ids9'=>[]
            ];
            $shu3 = ["ids" => [] , "cond"=>$cond, "name" => "数字3" , "type" => 0 , 
                0=>0 , 1=>0 , 2=>0 , 3=>0 , 4=>0 , 5=>0 , 6=>0 , 7=>0 , 8=>0 , 9=>0,
                'ids0'=>[], 'ids1'=>[], 'ids2'=>[], 'ids3'=>[], 'ids4'=>[], 'ids5'=>[], 'ids6'=>[], 'ids7'=>[], 'ids8'=>[], 'ids9'=>[]
            ];
            $dan = ["ids" => [] , "cond"=>$cond, "name" => "单" , "type" => 1 , 0=>0];
            $shuang = ["ids" => [] , "cond"=>$cond, "name" => "双" , "type" => 2 , 0=>0];
            $da = ["ids" => [] , "cond"=>$cond, "name" => "大" , "type" => 3 , 0=>0];
            $xiao = ["ids" => [] , "cond"=>$cond, "name" => "小" , "type" => 4 , 0=>0];
            $dadan = ["ids" => [] , "cond"=>$cond, "name" => "大单" , "type" => 5 , 0=>0];
            $xiaoshuang = ["ids" => [] , "cond"=>$cond, "name" => "小双" , "type" => 6 , 0=>0];
            $xiaodan = ["ids" => [] , "cond"=>$cond, "name" => "小单" , "type" => 7 , 0=>0];
            $dashuang = ["ids" => [] , "cond"=>$cond, "name" => "大双" , "type" => 8 , 0=>0];

            foreach($result as $k => $val) {

                $rmb = $val->num * $val->amounts * 95 / 100;

                switch ($val->type) {
                    case 0; {
                        $json = json_decode($val->res, true);

                        foreach ($json as $key => $v2) {

                            if ($key == 1) {
                                foreach ($v2 as $v) {
                                    $shu1[$v] = (int)($shu1[$v] * 100 + $rmb * 100) / 100;
//                                     $shu1[$v] += $rmb;
                                    $ids ='ids' . $v;
                                    if (!in_array($val->id, $shu1[$ids])) $shu1[$ids][] = $val->id;
                                }
                            } elseif ($key == 2) {
                                foreach ($v2 as $v) {
                                    $shu2[$v] = (int)($shu2[$v] * 100 + $rmb * 100) / 100;
                                    $ids ='ids' . $v;
                                    if (!in_array($val->id, $shu2[$ids])) $shu2[$ids][] = $val->id;
                                }
                            } elseif ($key == 3) {
                                foreach ($v2 as $v) {
                                    $shu3[$v] = (int)($shu3[$v] * 100 + $rmb * 100) / 100;
                                    $ids = 'ids' . $v;
                                    if (!in_array($val->id, $shu3[$ids])) $shu3[$ids][] = $val->id;
                                }
                            }
                        }

                        break;
                    }
                    case 1; { // 单
                        $dan[0] = (int)($dan[0] * 100 + $rmb * 100) / 100;
                        $dan["ids"][] = $val->id;
                        break;
                    }
                    case 2; { // 双
                        $shuang[0] = (int)($shuang[0] * 100 + $rmb * 100) / 100;
                        $shuang["ids"][] = $val->id;
                        break;
                    }
                    case 3; { // 大
                        $da[0] = (int)($da[0] * 100 + $rmb * 100) / 100;
                        $da["ids"][] = $val->id;
                        break;
                    }
                    case 4; { // 小
                        $xiao[0] = (int)($xiao[0] * 100 + $rmb * 100) / 100;
                        $xiao["ids"][] = $val->id;
                        break;
                    }
                    case 5; { // 大单
                        $dadan[0] = (int)($dadan[0] * 100 + $rmb * 100) / 100;
                        $dadan["ids"][] = $val->id;
                        break;
                    }
                    case 6; { // 小双
                        $xiaoshuang[0]  = (int)($xiaoshuang[0] * 100 + $rmb * 100) / 100;
                        $xiaoshuang["ids"][] = $val->id;
                        break;
                    }
                    case 7; { // 小单
                        $xiaodan[0]  = (int)($xiaodan[0] * 100 + $rmb * 100) / 100;
                        $xiaodan["ids"][] = $val->id;
                        break;
                    }
                    case 8; { // 大双
                        $dashuang[0]  = (int)($dashuang[0] * 100 + $rmb * 100) / 100;
                        $dashuang["ids"][] = $val->id;
                        break;
                    }
                }
            }

            $arr = [$shu1 , $shu2 , $shu3 , $da , $xiao , $dan , $shuang , $dadan , $xiaodan , $dashuang , $xiaoshuang];

            foreach ($arr as $k => $val) {
                $arr[$k]["ids"] = implode(",", $val["ids"]);
            }

//            $arr = [];
//            $arr1 = ['shu1'=>[],'shu2'=>[],'shu3'=>[]];
//            $arr2 = [];
//            $arr3 = ['shu1'=>[],'shu2'=>[],'shu3'=>[]];
//            foreach($result as $k=>$v) {
//                $arr[$k] = json_decode(json_encode($v),true);
//                if($arr[$k]['discount_type'] == 2){
//                    $shu = json_decode(str_replace(array('[',']'),array('"','"'),$arr[$k]['res']),true);
//                    foreach($shu as $kk=>$vv){
//                        $ve = explode(',',str_replace(' ','',$vv));
//                        $shu[$kk] = array();
//                        $shu1[$kk] = array();
//                        foreach($ve as $vvv){
//                            $shu[$kk][$vvv] = $arr[$k]['num']*$arr[$k]['amounts']*0.95;
//                            $shu1[$kk][$vvv] = $v->id;
//                        }
//                    }
//                    $arr1['shu1'][] = $shu[1];
//                    $arr1['shu2'][] = $shu[2];
//                    $arr1['shu3'][] = $shu[3];
//                    $arr3['shu1'][] = $shu1[1];
//                    $arr3['shu2'][] = $shu1[2];
//                    $arr3['shu3'][] = $shu1[3];
//                } else {
//                    $lx = $arr[$k]['discount_type'].$arr[$k]['type'];
//                    $arr[$k][$lx] = $arr[$k]['num']*$arr[$k]['amounts']*0.95;
//                    $arr2[$k][$lx] = $v->id;
//                }
//            }
//            foreach($arr1 as $k => $v) {
//                $collect = collect($arr1[$k]);
//                $ttr = '';
//                for($i=0;$i<=9;$i++) {
//                    $oids = collect($arr3[$k])->implode($i,',');
//                    $href = "/order/order?url=2&user_id=&_search=false&nd=1503045935302&rows=20&page=1&sidx=orders.id&sord=desc&oid=".$oids;
//                    $ttr .= "<td><a alt='' href='$href' target='_blank'>".$collect->sum($i)."</td>";
//                }
//                $arr1[$k] = $ttr;
//            }
//
//            $coll = collect($arr);
//            $coll2 = collect($arr2);
//
//            $xuanx = array('da'=>'03','xiao'=>'04','dan'=>'01','shuang'=>'02','dad'=>'15','xiaod'=>'17','das'=>'18','xiaos'=>'16');
//            foreach($xuanx as $k => $v) {
//                $arr1[$k] = "<a alt='' target='_blank' href='/order/order?url=2&user_id=&_search=false&nd=1503045935302&rows=20&page=1&sidx=orders.id&sord=desc&oid=".$coll2->implode($v,',')."'>".$coll->sum($v)."</a>";
//            }

            $data = [
                "rows" => $arr,
                "total"=> 1,
                "page" => 1,
                "records" => 1,
            ];
            return response()->json($data);
        } else {
            return view('lottery/touzhu');
        }
    }
}
